package com.hxkj.service.article.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.AnalyzeChannelServiceClient;
import com.hxkj.client.service.ChannelServiceClient;
import com.hxkj.client.service.ManuscriptServiceClient;
import com.hxkj.client.service.NavigationServiceClient;
import com.hxkj.domain.enums.ChannelExceptionEnum;
import com.hxkj.domain.po.Channel;
import com.hxkj.domain.po.FreeMarker;
import com.hxkj.domain.po.Manuscript;
import com.hxkj.domain.po.Navigation;
import com.hxkj.service.article.ChannelService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.hxkj.domain.Constant.CmsConstants.CHANNEL;

/**
 * @ClassName ChannelServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:59
 * @Version 1.0
 */

@Service
@Slf4j
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelServiceClient channelServiceClient;

    @Autowired
    private ManuscriptServiceClient manuscriptServiceClient;

    @Override
    public void saveChannel(Channel channel) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (channel == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_VOID);
            }
            if (null == channel.getChannelName() || channel.getChannelName() == "") {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_NAME);
            }
            if (channel.getSiteId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_SITEID);
            }
            if (channel.getTempletId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_TEMPLETID_NOT_NUll);
            }
            if (StringUtils.isEmpty(channel.getTempletName())) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_TEMPLETNAME_NOT_EXIST);
            }
            resultMessageStruct = channelServiceClient.saveChannel(channel);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(ChannelExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<Channel> getChannelList(Channel channel, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<Channel> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(channel);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<Channel> resultPageStruct;
        try {
            resultPageStruct = channelServiceClient.getChannelList(parameterDataStruct);

            //Mr.zheng:将频道静态资源路径封装进频道中
            List<Channel> channelList = resultPageStruct.getData();
            for (Channel cha : channelList) {
                Manuscript manuscript = new Manuscript();
                manuscript.setSiteId(cha.getSiteId());
                manuscript.setStatus(CHANNEL);//频道
                if (cha.getTempletName().equals("index")) {//根据别名判断是否为首页
                    manuscript.setAlisName("index");
                } else {
                    manuscript.setAlisName(manuscript.getStatus() + "" + cha.getSiteId() + "" + cha.getChannelId());
                }
                ResultDataStruct<Manuscript> resultDataStruct = manuscriptServiceClient.getManuscript(manuscript);
                if (resultDataStruct.getData() != null && StringUtils.isNotBlank(resultDataStruct.getData().getUrlPath())) {
                    cha.setManuscriptPath(resultDataStruct.getData().getUrlPath());
                }
            }
            resultPageStruct.setData(channelList);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取频道列表失败");
            throw e;
        }
        return resultPageStruct;
    }

    @Override
    public void removeChannel(Channel channel) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (channel == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_VOID);
            }
            if (channel.getSiteId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_SITEID);
            }
            if (channel.getChannelIds().equals("")) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_VOID);
            }
            //删除频道成功后删除对应的导航栏  chenfangzhou
            resultMessageStruct = channelServiceClient.removeChannel(channel);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(ChannelExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public void modifyChannel(Channel channel) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (channel == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_VOID);
            }
            if (channel.getChannelId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_VOID);
            }
            if (channel.getSiteId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_SITEID);
            }
            resultMessageStruct = channelServiceClient.modifyChannel(channel);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(ChannelExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
            //频道设置为不显示在导航栏,删除对应的导航栏
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    @Override
    public List<Channel> getAllChannel(FreeMarker freeMarker) {
        ResultDataStruct<List<Channel>> resultDataStruct = new ResultDataStruct<>();
        try {
            if (freeMarker.getSiteId() == null) {
                throw new CustomException(ChannelExceptionEnum.CHANNEL_NOT_SITEID);
            }
            Channel channel = new Channel();
            channel.setSiteId(freeMarker.getSiteId());
            channel.setDelTag((byte) 0);
            channel.setChannelStatus(0L);
            resultDataStruct = channelServiceClient.getAllChannel(channel);
            if (resultDataStruct.getCode() == null) {
                throw new CustomException(ChannelExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDataStruct.getData();
    }

    /**
     * 查询频道
     **/
    public List<Channel> getChannelList(Channel channel) {
        try {
            ResultDataStruct<List<Channel>> listResultDataStruct = channelServiceClient.getAllChannel(channel);
            List<Channel> channelList = listResultDataStruct.getData();
            return channelList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }


}
